/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Helper.SuppliersHepler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author KIENLT
 */
public class SuppliersDAO {
    public static int Insert(Connection cnn, SuppliersHepler obj) throws Exception{
        String strSQL = "INSERT INTO tblSuppliers(SName, Description) VALUES(?,?)";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getName());
        stmt.setString(2, obj.getDescription());
        return stmt.executeUpdate();
    }
    
    public static int Update(Connection cnn, SuppliersHepler obj) throws Exception{
        String strSQL = "UPDATE tblSuppliers SET SName=?, Description=? WHERE SID=?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getName());
        stmt.setString(2, obj.getDescription());
        stmt.setString(3, obj.getId());
        return stmt.executeUpdate();
    }
    
    public static int Delete(Connection cnn, SuppliersHepler obj) throws Exception{
        String strSQL = "DELETE FROM tblSuppliers WHERE SID = ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getId());
        return stmt.executeUpdate();
    }
    
   public static ResultSet FindOne(Connection cnn, SuppliersHepler obj) throws Exception {
        String strSQL = "SELECT * FROM tblSuppliers WHERE SID = ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getId());
        return stmt.executeQuery();
    }
    
    public static ArrayList<SuppliersHepler> FindAll(Connection cnn) throws Exception{
        ArrayList<SuppliersHepler> arrUserList = new ArrayList<SuppliersHepler>();
        SuppliersHepler userData = null;
        String strSQL = "SELECT * FROM tblSuppliers WHERE Status = 1";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            userData = new SuppliersHepler();
            userData.setId(rs.getString("SID"));
            userData.setName(rs.getString("SName"));
            userData.setDescription(rs.getString("Description"));
            arrUserList.add(userData);
        }
        return arrUserList;
    }
    
    public static ResultSet FillAll(Connection cnn) throws Exception {
        String strSQL = "SELECT * FROM tblSuppliers WHERE Status=1";
        CallableStatement stmt = cnn.prepareCall(strSQL);
        return stmt.executeQuery();
    }
}
